package love.yuanmengrobot.qdkaoqin.business;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Timer;
import java.util.TimerTask;

import love.yuanmengrobot.qdkaoqin.R;
import love.yuanmengrobot.qdkaoqin.entity.Member;
import love.yuanmengrobot.qdkaoqin.util.Constants;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {
    public static final String MASSAGE_STRING = "registerActivity_message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        String registerMessage = getIntent().getStringExtra(MainActivity.MESSAGE_STRING);
        TextView title = findViewById(R.id.register);
        title.setText(registerMessage);

        String permission;
        if (registerMessage.equals("学生注册")) {
            permission = "1";
        } else {
            permission = "0";
        }
        final String perm = permission;
        final EditText username = findViewById(R.id.register_username);
        final EditText password = findViewById(R.id.register_password);
        final EditText confirmPassword = findViewById(R.id.confirmPassword);
        final EditText name = findViewById(R.id.name);

        Button register = findViewById(R.id.registerAccount);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (password.getText().toString().equals(confirmPassword.getText().toString())) {
                    get(username.getText().toString(), password.getText().toString(), name.getText().toString(), perm);
                } else {
                    alertDialog("警告！", "两次输入密码不一致！");
                }
            }
        });

        //返回begin
        Button backToBegin = findViewById(R.id.backToBegin);
        backToBegin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//结束该活动
            }
        });
    }

    //定义一个弹窗提示
    public void alertDialog(String title, String content) {
        final AlertDialog alertDialog1 = new AlertDialog.Builder(RegisterActivity.this)
                .setTitle(title)//标题
                .setMessage(content)//内容
                .setIcon(R.mipmap.ic_warning)
                .create();
        alertDialog1.show();
        final Timer t = new Timer();//设置窗口自动关闭
        t.schedule(new TimerTask() {
            @Override
            public void run() {
                alertDialog1.dismiss();
                t.cancel();
            }
        }, 2000);//2秒后窗口自动关闭
    }

    private okhttp3.Callback callback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
//            Log.e(TAG, "Failed to connect server!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();
                System.out.println("register status...................." + body);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (body.contains("注册成功")) {
                            alertDialog("提示！", "注册成功！");
//                            finish();//结束注册活动
                        } else {
                            alertDialog("警告！", "用户名已存在！");
                        }
                    }
                });
            } else {
                alertDialog("错误！", "服务请求失败！");
            }
        }
    };

    public void get(final String username, final String password, final String name, final String perm) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FormBody.Builder builder = new FormBody.Builder();
                /* 添加四个参数 */
                builder.add("id", username)
                        .add("paw", password)
                        .add("permission", perm)
                        .add("name", name);
                FormBody body = builder.build();
//                String testUrl = "http://192.168.0.104:8080/register"
                Request request = new Request.Builder().url(Constants.REGISTER_URL).post(body).build();

                /* 下边和get一样了 */
                try {
                    OkHttpClient client = new OkHttpClient();
                    client.newCall(request).enqueue(callback);
                } catch (NetworkOnMainThreadException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }
}